package com.wh.controller;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.entity.Result;
import com.wh.mapper.StudentMapper;
import com.wh.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class StudentController {
        @Resource   //IOC  控制反转
        StudentMapper studentMapper;

        // handler 处理器
        @GetMapping("/all")
        public List<Student> selectAll(){
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.orderByDesc("sid");
                List<Student> students = studentMapper.selectList( wrapper );
                return students;
        }

        @GetMapping("/select/{major}")
        public List<Student> selectByMajor(@PathVariable String major){
                QueryWrapper<Student> wrapper = new QueryWrapper<>();  //查询条件
                wrapper.eq( "major",major);  //设置查询条件，指定的专业
                List<Student> students = studentMapper.selectList( wrapper );
                return students;
        }

        @GetMapping("/qq")
        public Student selectOne(){
                Student std;
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.eq("sid", 2); //查询2号学生
                std = studentMapper.selectOne(wrapper);
                return  std;
        }
        @GetMapping("/q1")
        public List<Student> selectList1(){
                List<Student> students;
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.eq("gender", "男").eq("major", "计算机");
                students = studentMapper.selectList(wrapper);
                return  students;
        }

        @GetMapping("/q2")
        public List<Student> selectList2(){
                List<Student> students;
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.between("age", 20, 25);
                students = studentMapper.selectList(wrapper);
                return  students;
        }

        @GetMapping("/q3")
        public List<Student> selectList3(){
                List<Student> students;
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.in("major", "计算机","机械工程");
                students = studentMapper.selectList(wrapper);
                return  students;
        }

        @PostMapping("/add")
       public Result add( @RequestBody  Student student ){
                studentMapper.insert(student);
                Result result = new Result(200, "添加学生成功", null);
                return  result;
        }

        @PutMapping("/update")
        public Result update( @RequestBody  Student student ){
                studentMapper.updateById(student);
                Result result = new Result(200, "修改学生成功", null);
                return  result;
        }
        @DeleteMapping("/delete/{sid}")
        public Result delete(@PathVariable Integer sid){
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("sid", sid);
                studentMapper.delete(wrapper);
                Result result = new Result(200, "删除学生成功", null);
                return  result;
        }

        @GetMapping("/page")
        public List<Student> page(){
                List<Student> students = null;
                Page<Student> page = new Page<>(2, 5); // 1表示第1页，5表示每页显示5条记录
                studentMapper.selectPage(page, null);  //无条件查询第1页的5条记录
                students =  page.getRecords();    //获取第1页的所有学生记录
                return students;
        }
        @GetMapping("/page/{cur}/{size}")
        public List<Student> page2(@PathVariable Integer cur, @PathVariable Integer size){
                List<Student> students = null;
                Page<Student> page = new Page<>(cur, size);
                studentMapper.selectPage(page, null);
                students = page.getRecords();  //获取当前页的学生记录
                return students;
        }
        @GetMapping("/page/{cur}/{size}/{major}")
        public List<Student> page3(@PathVariable Integer cur, @PathVariable Integer size, @PathVariable String major){
                List<Student> students = null;
                Page<Student> page = new Page<>(cur, size);
                QueryWrapper<Student> wrapper =null;

                if(!"".equals(major.trim())){
                        wrapper = new QueryWrapper<>();
                        wrapper.eq("major", major);
                }

                studentMapper.selectPage(page, wrapper); //查询指定专业的学生，并分页
                students = page.getRecords();  //获取当前页的学生记录
                return students;
        }




}
